﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_ProductCategoryEdit : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }


    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        BindFather();
        ProductCategories = Config.Id > 0 ? new ProductCategories { CateId = Config.Id }.ProductCategories_GetByCateId() :
        new ProductCategories();
        CheckPermission();

    }


    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {

        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
    }


    /// <summary>
    /// 
    /// </summary>
    protected ProductCategories ProductCategories
    {
        get
        {
            var productCategories = (ProductCategories)Session[SessionManager.SessionData] ?? new ProductCategories();
            productCategories.FatherID = int.Parse(ddlFather.SelectedValue);
            productCategories.CateName = txtName.Text;
            productCategories.Alias = txtAlias.Text;
            productCategories.Status = cbActive.Checked;
            return productCategories;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtAlias.Text = value.Alias;
            txtName.Text = value.CateName;
            cbActive.Checked = value.Status;
            ddlFather.SelectedValue = value.FatherID.ToString();
        }
    }




    /// <summary>
    /// 
    /// </summary>
    private void BindFather()
    {
        ddlFather.Items.Clear();
        ddlFather.Items.Add(new ListItem { Value = @"0", Text = @"--- Select father category ---" });
        var list = new ProductCategories().ProductCategories_GetByCondition(" status=1");
        foreach (var categoriese in list)
            ddlFather.Items.Add(new ListItem { Value = categoriese.CateId.ToString(), Text = categoriese.CateName });

    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                ProductCategories = new ProductCategories();
                Config.Id = 0;
                BindFather();
                break;
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                var error = ProductCategories.Save();
                if (error > 0)
                {
                    ProductCategories.CateId = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.NewsCategory), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }


}
